<template>
  <div class="ingredient-list" v-if="ingredients.length > 0">
    <h3>所需食材：</h3>
    <ul class="ingredients">
      <li 
        v-for="(ingredient, index) in ingredients" 
        :key="index" 
        class="ingredient-item"
      >
        <span class="ingredient-name">{{ ingredient.name }}</span>
        <span class="ingredient-quantity">{{ ingredient.quantity }}</span>
      </li>
    </ul>
    <button @click="addToShoppingList" class="add-to-list-button">
      添加到购物清单
    </button>
  </div>
  
  <div class="no-ingredients" v-else-if="analyzed">
    <p>未找到相关食材信息，请尝试其他菜品。</p>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';

const props = defineProps({
  ingredients: {
    type: Array,
    default: () => []
  },
  analyzed: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['add-to-shopping-list']);

const addToShoppingList = () => {
  if (props.ingredients.length > 0) {
    emit('add-to-shopping-list', props.ingredients);
  }
};
</script>

<style scoped>
.ingredient-list {
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
}

.ingredient-list h3 {
  margin-bottom: 15px;
  color: #333;
}

.ingredients {
  list-style: none;
  padding: 0;
  margin: 0 0 20px 0;
  text-align: left;
  display: inline-block;
}

.ingredient-item {
  padding: 8px 15px;
  margin: 5px 0;
  background-color: #f8f9fa;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
}

.ingredient-name {
  font-weight: 500;
}

.ingredient-quantity {
  color: #666;
}

.add-to-list-button {
  padding: 10px 20px;
  background-color: #42b883;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.add-to-list-button:hover {
  background-color: #36986f;
}

.no-ingredients {
  padding: 20px;
  text-align: center;
  color: #666;
}
</style>
